package com.kws.payment.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.kws.payment.business.AlipayBusiness;
import com.kws.payment.dao.AlipayRefundOrderRq;
import com.kws.payment.dao.AlipayWebRq;
import com.kws.payment.service.AlipayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <b>AlipayServiceImpl</b>
 *
 * @description: AlipayServiceImpl <br>
 * @date: 2024/10/31 09:57 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class AlipayServiceImpl implements AlipayService {
    private static final Logger logger = LoggerFactory.getLogger(AlipayServiceImpl.class);

    @Autowired
    private AlipayBusiness alipayBusiness;

    @Override
    public String alipayWebPay(AlipayWebRq rq) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo(rq.getOrderNo());
        model.setTotalAmount(rq.getAmount().toString());
        model.setSubject(rq.getSubject());
        model.setProductCode("QUICK_WAP_WAY");
        request.setBizModel(model);
        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "POST");
        String pageRedirectionData = response.getBody();
        logger.info("获取支付宝H5支付返回下单拉起支付返回结果:{}"+pageRedirectionData);
        if (response.isSuccess()){
            return pageRedirectionData;
        }else {
            throw  new SecurityException("调用支付宝H5下单拉起支付失败");
        }
    }

    @Override
    public String refundOrder(AlipayRefundOrderRq rq) throws AlipayApiException {
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        AlipayTradeRefundModel model = new AlipayTradeRefundModel();
        model.setOutTradeNo(rq.getOderNo());
        model.setRefundAmount(rq.getRefundAmount());
        request.setBizModel(model);
        AlipayTradeRefundResponse response = alipayBusiness.alipayClient().execute(request);
       logger.info("调用退款返回结果:"+response.getBody());
        if (response.isSuccess()) {
            return response.getBody();
        } else {
            throw new SecurityException("退款失败");
        }
    }

    @Override
    public String createAlipayH5Order(AlipayWebRq rq) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo(rq.getOrderNo());
        model.setTotalAmount(rq.getAmount().toString());
        model.setSubject(rq.getSubject());
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        request.setBizModel(model);
        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "POST");
        String pageRedirectionData = response.getBody();
        logger.info("获取支付宝H5支付返回下单拉起支付返回结果:{}"+pageRedirectionData);
        if (response.isSuccess()){
            return pageRedirectionData;
        }else {
            throw  new SecurityException("调用支付宝H5下单拉起支付失败");
        }
    }

    @Override
    public boolean cancelAlipayOrder(String orderNo) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        JSONObject requestJson = new JSONObject();
        requestJson.put("out_trade_no",orderNo);
        request.setBizContent(requestJson.toString());
        AlipayTradeCloseResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            logger.info("调用成功");
            return true;
        } else {
            logger.info("调用失败");
            return false;
        }
    }


    public AlipayConfig getAlipayConfig(){
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl(alipayBusiness.getGATEWAY_URL());
        alipayConfig.setAppId(alipayBusiness.getAPPID());
        alipayConfig.setAlipayPublicKey(alipayBusiness.getPUBLIC_KEY());
        alipayConfig.setPrivateKey(alipayBusiness.getMERCHANT_PRIVATE_KEY());
        alipayConfig.setFormat("json");
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}
